<?php
/**
 * what does this script do?
 * user flow:
 * 1. user chooses image
 * 2. user crops image
 * 3. user shown new image
 * 
 * script flow:
 * 1. displaying choose file input
 * 2. taking input (resizing if necessary) and outputting the new image to be cropped by the user
 * 3. processing image with provided dimensions and coords, display cropped image
 * 
 * @package Image2
 */
 
require("./includes/header.inc.php");

/**
 * setting the constants
 * -> on, off
 */
define("ON", true);
define("OFF", false);

/**
 * loading the settings
 * -> image upload restrictions (image_constraints), crop dimensions (crop_dimensions), image width after uploading (initial_image_width)
 * -> cropped name format (cropped_name_format)
 */
$settings = parse_ini_file("./includes/settings.ini", true);
$image_constraints = $settings["image_constraints"];
$crop_dimensions = $settings["crop_dimensions"];
foreach ($crop_dimensions as $key => $value) $crop_dimensions[$key] = (int) $value;
$upload_directory = $settings["general"]["upload_directory"];
$initial_image_width = (int) $settings["general"]["initial_image_width"];
$initial_name_format = $settings["general"]["initial_name_format"];
$cropped_name_format = $settings["general"]["cropped_name_format"];

/**
 * setting the main script objects
 * -> user, script, page
 */
// contains user actions
$user = new stdClass;

// contains script actions and variables
$script = new stdClass;
$script -> debug_mode = OFF;

// contains page variables
$page = new stdClass;
$page -> debug_data = array();
$page -> errors = array();
$page -> messages = array();

/**
 * processing the inputs
 */
if (isset($_POST["image_name"], $_POST["image_location"], $_POST["x1"], $_POST["x2"], $_POST["y1"], $_POST["y2"]))
{
	$image_location = $_POST["image_location"];
	try
	{
		$Uploaded_Image = new Image2($image_location);
	}
	catch(NotAnImageException $e)
	{
		$page -> errors[] = "Not an image";
	}
	catch (InvalidImageException $e)
	{
		$page -> errors[] = "Invalid image";
	}
	
	if (count($page -> errors) == 0)
	{
		// -- script flow variable assignment --
		// cropping_image flag
		$script -> cropping_image = true;
		// Uploaded_Image object
		$script -> uploaded_image = $Uploaded_Image;
		// crop_dimensions
		$script -> crop_coordinates = array("x1" => (int) $_POST["x1"], "x2" => (int) $_POST["x2"], "y1" => (int) $_POST["y1"], "y2" => (int) $_POST["y2"]);
		// image_name
		$script -> image_name = $_POST["image_name"];
	}
}
if (isset($_FILES["new_image"]))
{
	$image_location = $_FILES["new_image"]["tmp_name"];
	try
	{
		$Uploaded_Image = new Image2($image_location);
	}
	catch(NotAnImageException $e)
	{
		$page -> errors[] = "Not an image";
	}
	catch (InvalidImageException $e)
	{
		$page -> errors[] = "Invalid image";
	}
	
	// -- image validation --
	if (isset($Uploaded_Image) && $Uploaded_Image -> isImage() == true)
	{
		if ($Uploaded_Image -> getWidth() < $image_constraints["min_width"])
		{
			$page -> errors[] = "Image width too small (minimum image width: {$image_constraints["min_width"]}px, given image's width: ". $Uploaded_Image -> getWidth(). "px)";
		}
		if ($Uploaded_Image -> getHeight() < $image_constraints["min_height"])
		{
			$page -> errors[] = "Image height too small (minimum image width: {$image_constraints["min_height"]}px, given image's width: ". $Uploaded_Image -> getHeight(). "px)";
		}
	}
	
	if (count($page -> errors) == 0)
	{
		// -- script flow variable assignment --
		// initiate_cropping flag
		$script -> initiate_cropping = true;
		// Uploaded_Image object
		$script -> uploaded_image = $Uploaded_Image;
	}
}

/**
 * deciding the script flow path
 * -> cropping_image, initiate_cropping
 */
if (isset($script -> cropping_image))
{
	$Uploaded_Image = $script -> uploaded_image;
	$Uploaded_Image -> crop($crop_dimensions, $script -> crop_coordinates) -> saveToFile($upload_directory. sprintf($cropped_name_format, $script -> image_name)) -> destroyResource();
	
	// -- user action flags and page variable assignment --
	// viewing_image flag
	$user -> viewing_image = true;
	// page messages
	$page -> messages[] = "Image cropped successfully";
	// image_location
	$page -> image_location = $Uploaded_Image -> getFileLocation();
}
if (isset($script -> initiate_cropping))
{
	$Uploaded_Image = $script -> uploaded_image;
	$current_timestamp = strtotime(date("Y-m-d H:i:s"));
	$Uploaded_Image -> resize(array("width" => $initial_image_width)) -> saveToFile($upload_directory. sprintf($initial_name_format, $current_timestamp)) -> destroyResource();
	
	$language = "php";
	$example_code = '<?php

$image_location = $_FILES["new_image"]["tmp_name"];
try
{
	$Uploaded_Image = new Image2($image_location);
}
catch (NotAnImageException $e)
{
}
catch (InvalidImageException $e)
{
}

if (isset($Uploaded_Image) && $Uploaded_Image -> isImage())
{
	$dimensions = array("width" => 600);
	$file_path = sprintf("./upload_pic/original_%s", strtotime(date("Y-m-d H:i:s")));
	
	$Uploaded_Image -> resize($dimensions) -> saveToFile($file_path);
}

?>';
	$geshi = new GeSHi($example_code, $language);
	
	// -- user action flags and page variable assignment --
	// cropping_image flag
	$user -> cropping_image = true;
	// page messages
	$page -> messages[] = "Image uploaded successfully";
	// new image location
	$page -> image_location = $Uploaded_Image -> getFileLocation();
	$page -> image_name = $current_timestamp;
	// new image dimensions
	$page -> image_width = $Uploaded_Image -> getWidth();
	$page -> image_height = $Uploaded_Image -> getHeight();
	// crop image dimensions
	$page -> crop_width = $crop_dimensions["width"];
	$page -> crop_height = $crop_dimensions["height"];
	// code example
	$page -> code_example = $geshi -> parse_code();
	// debug data
	$page -> debug_data["uploaded_image_object"] = $Uploaded_Image;
}
else
{
	$user -> choosing_image = true;
}

require("./templates/index.php");